#include "vxscaletrigger.h"

class ZoomTransition: public QSignalTransition
{
	Q_OBJECT
public:
	ZoomTransition(VxZoomController * zcontroller, int threshold, bool sunkToAbove) :
			QSignalTransition(zcontroller, SIGNAL(scaleShanged(int))),
			_threshold(threshold), _sunkToAbove(sunkToAbove)
	{
	}

	bool eventTest(QEvent *event)
	{
		if (!QSignalTransition::eventTest(event)){
			return false;
		}

		QStateMachine::SignalEvent * se = static_cast<QStateMachine::SignalEvent *>(event);
		int scale = se->arguments().at(0).toInt();


		if (_sunkToAbove){
			return scale >= _threshold;
		} else {
			return scale < _threshold;
		}

	}

private:
	int _threshold;
	bool _sunkToAbove;

};


VxScaleTrigger::VxScaleTrigger(VxZoomController * controller, int threshold) :
		QStateMachine::QStateMachine()
{
	QState * sunk  = new QState(this);
	QState * above = new QState(this);
	QState * undef = new QState(this);

	ZoomTransition * sunkToAbove = new ZoomTransition(controller, threshold, true);
	sunkToAbove->setTargetState(above);
	sunk->addTransition(sunkToAbove);

	ZoomTransition * aboveToSunk = new ZoomTransition(controller, threshold, false);
	aboveToSunk->setTargetState(sunk);
	above->addTransition(aboveToSunk);

	ZoomTransition * undefToAbove = new ZoomTransition(controller, threshold, true);
	undefToAbove->setTargetState(above);
	undef->addTransition(undefToAbove);

	ZoomTransition * undefToSunk = new ZoomTransition(controller, threshold, false);
	undefToSunk->setTargetState(sunk);
	undef->addTransition(undefToSunk);

	connect(sunkToAbove, SIGNAL(triggered()), this, SIGNAL(above()));
	connect(aboveToSunk, SIGNAL(triggered()), this, SIGNAL(sunk()));

	connect(undefToAbove, SIGNAL(triggered()), this, SIGNAL(above()));
	connect(undefToSunk, SIGNAL(triggered()), this, SIGNAL(sunk()));

	setInitialState(undef);

	start();
};




#include "vxscaletrigger.moc"
